package work.sajor.wechatpush.service;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import work.sajor.wechatpush.controller.WechatController;
import work.sajor.wechatpush.entiy.Text;
import work.sajor.wechatpush.entiy.msg;
import work.sajor.wechatpush.util.HttpUtil;
import work.sajor.wechatpush.util.WechatUtils;

/**
 * @ClassName Tianqi
 * @Description TODO
 * @Author ydzhao
 * @Date 2022/8/2 16:45
 */
@Service
public class SendMsg {

   public static String accessToken = "60_20DNFOV5Mi7q1Cj77zzIYr_v0qygZRXKGApnXLpqYoz1M4comv-nEJtAb0YKckP96IJrtx7eLHk9rsVXwFIgt6mgr0OJcYNAUJpqoAoyihwNpIcRl-HwkwVB_UVf4QgBkCcfY_QArr6P0IXwYTWiAAAKFI";

    @Autowired
    WechatUtils wechatUtils;

//    @Value("${target.test.openId}")
//    private String openId;

    public void sendMsg(String openId, String message) {
        String result = null;
        try {
//            String accessToken = wechatUtils.getAccessToken();
            msg msg = new msg();
            msg.setTouser(openId);
            msg.setMsgtype("text");
            msg.setText(new Text(message));
            String jsonString = JSONObject.toJSONString(msg);
            System.out.println("消息"+openId+":" + message);
            result = HttpUtil.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken,jsonString);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("errcode").equals("0")) {
                System.out.println("消息发送成功 "+openId+":" + message);
            }else {
                System.out.println(" 重新获取token:"+accessToken);
                accessToken=wechatUtils.getAccessToken();
                result = HttpUtil.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken,jsonString);
                JSONObject jsonObject2 = JSONObject.parseObject(result);
                if (jsonObject2.getString("errcode").equals("0")) {
                    System.out.println("消息发送成功 "+openId+":" + message);
                }else {
                    System.out.println("消息发送成功 "+openId+":" + message);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void sendMessage(msg message) {
        String result = null;
        try {
//            String accessToken = wechatUtils.getAccessToken();
//            String accessToken = "60_20DNFOV5Mi7q1Cj77zzIYr_v0qygZRXKGApnXLpqYoz1M4comv-nEJtAb0YKckP96IJrtx7eLHk9rsVXwFIgt6mgr0OJcYNAUJpqoAoyihwNpIcRl-HwkwVB_UVf4QgBkCcfY_QArr6P0IXwYTWiAAAKFI";
            String jsonString = JSONObject.toJSONString(message);
            result = HttpUtil.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken,jsonString);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if (jsonObject.getString("errcode").equals("0")) {
                System.out.println("消息发送成功1 "+message.getTouser()+":" + message);
            }else {
                accessToken=wechatUtils.getAccessToken();
                result = HttpUtil.doPost("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accessToken,jsonString);
                JSONObject jsonObject2 = JSONObject.parseObject(result);
                if (jsonObject2.getString("errcode").equals("0")) {
                    System.out.println("消息发送成功2 "+message.getTouser()+":" + message);
                }else {
                    System.out.println("消息发送失败 "+message.getTouser()+":" + message);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
